package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        // 构建查询条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        // 如果传入的频道名称不为空,加入频道查询条件
        if (!StringUtils.isEmpty(dto.getName().trim())) {
            query.like(WmChannel::getName, dto.getName());
        }
        // 构建分页条件
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        // 分页查询
        IPage<WmChannel> iPage = this.page(page, query);
        // 返回通用的分页响应对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),
                iPage.getTotal(), iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult saveWmChannel(WmChannel entity) {
        // 参数判断
        if (entity == null || StringUtils.isEmpty(entity.getName().trim())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 新增频道
        // 判断频道名称在数据库中是否已经存在
        // 根据频道名称查询
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName, entity.getName());
        int count = this.count(query);
        // 已经存在提示已存在
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 否则保存新的频道
        entity.setIsDefault(true);
        entity.setCreatedTime(new Date());
        this.save(entity);
        return ResponseResult.okResult();
    }

    @Autowired
    private IWmNewsService newsService;
    @Override
    public ResponseResult updateWmChannel(WmChannel dto) {
        // 参数判断
        if (dto == null || StringUtils.isEmpty(dto.getName().trim())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 更新频道
        WmChannel channel = new WmChannel();
        if (dto.getId() != null) {
            channel.setId(dto.getId());
        }
        if (!StringUtils.isEmpty(dto.getName())) {
            LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
            query.eq(WmChannel::getName, dto.getName());
            WmChannel one = this.getOne(query);
            if (one == null || one.getId() == dto.getId()) {
                channel.setName(dto.getName());
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        if (dto.getDescription() != null) {
            channel.setDescription(dto.getDescription());
        }
        if (dto.getStatus()) {
            channel.setStatus(true);
        }else {
            LambdaQueryWrapper<WmNews> query = new LambdaQueryWrapper<>();
            query.eq(WmNews::getChannelId, dto.getId());
            int count = newsService.count(query);
            if (count > 0) {
                return ResponseResult.errorResult(1001,"频道被引用");
            }
            channel.setStatus(false);
        }
        if (dto.getOrd() != null) {
            channel.setOrd(dto.getOrd());
        }
        this.updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        WmChannel channel = this.getById(id);
        if (channel.getStatus()) {
            return ResponseResult.errorResult(1002, "只有禁用的频道才能删除");
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        if (wmChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName, wmChannel.getName());
        WmChannel one = this.getOne(queryWrapper);

        //如果不为空已经存在同名的频道名称，停止更新操作
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //TODO 判断当前频道下是否有数据，有数据不能修改为禁用
        //TODO 重新定义DTO对象，修改保存方法
        if (!wmChannel.getIsDefault()) {
            LambdaQueryWrapper<WmNews> newsQueryWrapper = new LambdaQueryWrapper<>();
            List<WmNews> wmNewsList = newsService.list(newsQueryWrapper);
            if (!CollectionUtils.isEmpty(wmNewsList)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXISTS_UNDER_THE_CHANEL);
            }
        }
        this.updateById(wmChannel);
        return ResponseResult.okResult();
    }
}
